<?php
class Henry_Gioithieu_Gioithieu extends Zend_Db_Table_Abstract
{
  	protected $_name = '' ;
  	protected $_primary = 'clip_id';
    protected $_rowClass = 'Henry_Gioithieu_GioithieuRow';
    const SHOW_INHOME_PAGE = "p1";
    const SHOW_INPROMOTION = "p2";
	private $clipProperties ;
    public function __construct() {
        parent::__construct();
        $dbconfig = $this->_db->getConfig();
        $this->_name = $dbconfig['prefix'].'gioithieu';
        $this->clipProperties = array(
        	self::SHOW_INHOME_PAGE => "Hiện Trên Trang Chủ",
        	self::SHOW_INPROMOTION => "Khuyến Mãi",
        );
    }
    public function getClipProperties(){
    	return $this->clipProperties;
    }
    private function urlProcess($title,$lang){
    	if (in_array($lang, Henry_Global::getChangFixCharacter())){
    		$title = Henry_String_FixviCharacter::change($title);
    	}
    	$catUrl = $this->getCategoriesUrl();
    	$result = array();
    	$matchCh = array();
    	foreach ($catUrl as $item){
    		if (preg_match("/".$title."-?(\d*$)/i",$item['categories_url'],$matchCh)){
    			$result[]=$matchCh[1];
    		}
    	}
    	if (count($result)>0){
    		$maxValue = max($result);
    		if ($maxValue==""||$maxValue==0) return $title.'-1';
    		else return $title.'-'.($maxValue+1);
    	} else { 
    		return $title;
    	}	
    }
    public function addCategories($data){
    	$this->fiterData($data);
    	return $this->insert($data);
    }
    public function updateCategories($data,$id){
    	$this->fiterData($data);
    	$where = $this->_db->quoteInto("clip_id = ?",$id);
		return $this->update($data, $where);
    }
    public function getTotalRecord($lang = Henry_Global::CMS_DEFAULT_LANG){
    	$select = $this->select()
    				   ->from($this->_name, array('totalCount'=>new Zend_Db_Expr("count(categories_id)")))
    				   ->where("language = ?",$lang);
 		return $this->_db->fetchOne($select);   
    }
    public function getCatbyParent($parentId = 0){
    	$where = $this->_db->quoteInto("parent_id = ?", $parentId);
    	return $this->fetchAll($where,array("categories_order desc","categories_title"))->toArray();
    }
    public function delRecode($id){
    	$where = $this->_db->quoteInto($this->_primary." = ?", $id);
    	return $this->delete($where);
    }
    private function fiterData(&$data){
    	foreach ($data as $key => $item){
    		if (!in_array($key, $this->_getCols())){ 
    			unset($data[$key]);
    		} else if (!preg_match("/clip_des[0-9]+/", $key)) {
    			$this->killInjaction($data[$key]);
    		}else{                    
                    if ( get_magic_quotes_gpc() )
			$data[$key] = htmlspecialchars( stripslashes($data[$key]),ENT_QUOTES, 'UTf-8');
                    else
			$data[$key] = htmlspecialchars( $data[$key],ENT_QUOTES, 'UTf-8');                   
                }
    	}
    }
    
    private function killInjaction(&$value){
    	$value = htmlentities($value,ENT_QUOTES,"UTF-8");
    	$value = mysql_escape_string($value);
    }
    public function getCatTree($lg='vn'){
    	return $this->processToGetTree($lg);	
    }
    private function processToGetTree($lg,$id=0,$sysbol = ""){
    	$result = array();
    	$select = $this->select()
    				   ->from($this->_name, array('categories_id','categories_title'))
    				   ->where("parent_id = ?", $id)
    				   ->where("language = ?", $lg)
    				   ->order(array("categories_order desc","categories_title asc"));
    	$data = $this->_db->fetchAll($select,Zend_Db::FETCH_ASSOC);
    	if (count($data)>0){
    		foreach ($data as $item){
    			$tmp['id'] = $item['categories_id'];
    			$tmp['title'] = $sysbol.$item['categories_title'];
    			$result[]=$tmp;
    			$childResult = $this->processToGetTree($lg,$item['categories_id'],"-- ".$sysbol);
    			if (count($childResult)>0){
    				foreach ($childResult as $childResultItem){
    					$result[] = $childResultItem;
    				}
    			}
    		}
    	}
    	return $result;
    }
    public function getCategoriesUrl(){
    	$select = $this->select()
    				   ->from($this->_name, array('categories_url'));
    	return $this->_db->fetchAll($select,Zend_Db::FETCH_ASSOC);			   
    }
    //-----------------
    public function getCatelist($lang = Henry_Global::CMS_DEFAULT_LANG,Array $cols = null,$where = null, $order = null, $count = null, $offset = null){
    	$select = $this->select()->from(array('c1'=>$this->_name), $cols)
    							->joinLeft(array('c2'=>$this->_name), "c1.parent_id = c2.categories_id", array("parent_cat_title" => "c2.categories_title"))
    							->where("c1.language = ?",$lang)
    							->order($order)
    							->limit($count,$offset);
    	return $this->_db->fetchAll($select);
    }
    
    public function getCatListExt($order,$start,$limit,$dir,$lang = 'vi'){
    	$select = $this->_db->select();
    	$select->from($this->_name,'*')
    	       ->where("language =?",$lang)
    	       ->order($order.' '.$dir)
    	       ->limit($limit,$start);
    	return $this->_db->fetchAll($select, Zend_Db::FETCH_ASSOC);        
    }
    public function getTotal($lang='vi'){
    	$select = $this->_db->select();
    	$select->from($this->_name,array(new Zend_Db_Expr('count(*)')))
    	       ->where("language =?",$lang);
    	return $this->_db->fetchOne($select);
    }
}